from rest_framework import serializers
from survey.models import Survey, Question, AnswerSet
from rest_framework.renderers import JSONRenderer
# import survey.views

class SurveyDetailSerializer(serializers.Serializer):
    survey_id = serializers.CharField(read_only=True)
    survey_name = serializers.CharField()
    survey_status = serializers.CharField()
    created = serializers.DateTimeField(read_only=True)
    updated = serializers.DateTimeField()

    question_list = serializers.JSONField()

    def to_representation(self, survey):
        serializer = QuestionSerializer(self.context['question_set'], many=True)
        # question_json = JSONRenderer().render(serializer.validated_data)
        return {
            'survey_id': survey.survey_id,
            'survey_name': survey.survey_name,
            'survey_status': survey.survey_status,
            'created': survey.created,
            'updated': survey.updated,
            'question_list': serializer.data
        }

class SurveySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Survey
        fields = ['survey_id', 'survey_name', 'survey_status',
                  'created', 'updated']

class QuestionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Question
        fields = ['qid', 'qtext', 'qtype',
                  'qnum', 'required', 'fill_others',
                  'options', 'survey_id']

class AnswerSetSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = AnswerSet
        fields = ['survey_id', 'student_id', 'answer',
                  'start_time', 'end_time', 'created']
